//
//  AFNblock.m
//  UI第三方小练习
//
//  Created by apple on 15/8/24.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "AFNblock.h"

@implementation AFNblock

+ (void)getdataWithString:(NSString *)string block:(AFNData)block
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
        NSString *url_string = [NSString stringWithFormat:@"%@",string];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [netWorkManager stopMonitoring];
        
        block(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        
//            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前网络不好" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alertView show];
//        });
        
        
    }];

    
    }


// 首先判断是否有缓存 有缓存则直接应用缓存 并在有网的情况下实时更新缓存 适用于数据不经常更新的情况
+(void)getdataWithUrlString:(NSString *)string dataBlock:(AFNData)block netBlock:(AFNData)netBlock
{
    // 首先获取缓存
    NSDictionary *dic = [NSDictionary dictionaryWithDictionary:[HCache getCacheWithPassword:string Type:1]];
    
    // 如若缓存不为空 则应用dataBlock缓存里面的内容dic
    if ([HCache getCacheWithPassword:string Type:1]) {
        block(dic);
    }
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = [NSString stringWithFormat:@"%@",string];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
 // 更新缓存,先remove.responseObject是网络请求回来的数据
        [HCache removeCacheWithPassword:string Type:1];
 // 然后重新将请求的数据存进去
        [HCache saveCacheWithPassword:string Type:1 Object:responseObject];
        
        // 如果不存在缓存 则用网络请求,请求回来的数据responseObject
        if (dic.allKeys.count == 0) {
            
            netBlock(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 如果没有可用缓存也没有网 则报无网错误提示
        if (dic.allKeys.count == 0) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"木有网" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
            });
        }
        
        
    }];
    
}

// 仅当目的地页面使用方法时使用(和目的地请求回来的数据有关)
+(void)getdataWithUrlString:(NSString *)string type:(NSInteger)type dataBlock:(AFNData)block netBlock:(AFNData)netBlock
{
    // 首先获取缓存
    NSDictionary *dic = [NSDictionary dictionaryWithDictionary:[HCache getCacheWithPassword:string Type:type]];
    
    // 如若不为空 则应用缓存
    if ([HCache getCacheWithPassword:string Type:type]) {
        block(dic);
    }
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = [NSString stringWithFormat:@"%@",string];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        // 更新缓存,先remove
        [HCache removeCacheWithPassword:string Type:type];
        // 然后重新将请求的数据存进去
        [HCache saveCacheWithPassword:string Type:type Object:responseObject];
        
        // 如果不存在缓存 则用网络请求的数据创建
        if (dic.allKeys.count == 0) {
            
            netBlock(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 如果没有可用缓存 则报无网错误提示
        if (dic.allKeys.count == 0) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"木有网" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
            });
        }
        
        
    }];
    
}
    
    



@end
